package net.zjitc.demo1.Controller;

import net.zjitc.demo1.pojo.*;
import net.zjitc.demo1.service.US;
import net.zjitc.demo1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users1")
public class UC {
    @Autowired
    private US userService;
    @PostMapping
    public R add(@RequestBody User user){
        try {
            userService.add(user);
        } catch (Exception e) {
            return new R(false, Message.ADD_USER_FAIL);
        }
        return new R(true, Message.ADD_USER_SUCCESS);
    }
    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") int id){
        try {
            userService.delete(id);
        } catch (Exception e) {
            return new R(false, Message.DELETE_USER_FAIL);
        }
        return new R(true, Message.DELETE_USER_SUCCESS);
    }
    @PutMapping
    public R update(@RequestBody User user){
        try {
            userService.update(user);
        } catch (Exception e) {
            return new R(false, Message.EDIT_USER_FAIL);
        }
        return new R(true, Message.EDIT_USER_SUCCESS);
    }
    @PostMapping("/{pageCurrent}/{pageSize}")
    public R find(@PathVariable("pageCurrent") int pageCurrent,@PathVariable("pageSize") int pageSize,
                  @RequestBody User user){
        RP<User> page=null;
        try {
            page = userService.find(pageCurrent, pageSize, user);
        } catch (Exception e) {
            return new R(false, Message.QUERY_USER_FAIL,null);
        }
        return new R(true, Message.QUERY_USER_SUCCESS,page);
    }
}
